#coding.:utf-8
from twisted.words.xish import domish

class Derrived(object):
        def createPresenceNode(self,priority,status=None,show=None,):
                presence=domish.Element((None,"presence"))
                presence.addElement("priority",content=str(priority))
                if not status is None:
                        presence.addElement("status",content=status)
                if not show is None:
                        presence.addElement("show",content=show)
                return presence
                
	def joinConference(self,conference):
                assert conference in self.conferencesConfigs , "not found the configuration of conference {0}".format(conference)
                password = self.conferencesConfigs[conference].get("password")
                nick     = self.conferencesConfigs[conference].get("nick")
		show   = self.conferencesConfigs[conference].get("show")
		status  = self.conferencesConfigs[conference].get("status")
		priority = self.conferencesConfigs[conference].get("priority") or 0
		prsJoin = self.createPresenceNode(priority,status,show)
		prsJoin["to"] = "{conference}/{nick}".format(**locals())
		mucTag = prsJoin.addElement("x","http://jabber.org/protocol/muc")
		historyTag = mucTag.addElement("history")
		historyTag["maxchars"]="0"
		historyTag["maxstansas"]="0"
		if password:
			mucTag.addElement("password",content=password)
		self.send(prsJoin)

	def leaveConference(self,conference,status=None):
		presence = domish.Element((None,"presence"))
		presence["type"] = "unavailable"
		presence["to"] = conference
		if status:
			presence.addElement("status",content=status)
		self.send(presence)
		self.freeConference(conference)

	def getAccess(self,jid,conference = None):
		gAcc = self.access.globals.get(jid)
		if gAcc:
			return gAcc
		if conference:
			if self.conferences.has_key(conference):
				gPermAcc = self.access.locals[conference].get(jid)
				if gPermAcc:
					return gPermAcc
		else:
			return 10
		 return 0